package de.jmda.mview.proj;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import de.jmda.mview.DesktopPane;
import de.jmda.util.gui.awt.graphics.IconProvider;

public class DiagramsTreeCellRenderer
    extends DefaultTreeCellRenderer
//    extends JLabel
//    implements TreeCellRenderer
{
  private static final long serialVersionUID = 1L;

	/**
	 * @see javax.swing.tree.DefaultTreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int, boolean)
	 */
	@Override
  public Component getTreeCellRendererComponent(
  		JTree tree, Object value, boolean sel, boolean expanded, boolean leaf,
  		int row, boolean hasFocus)
  {
		Component result =
				super.getTreeCellRendererComponent(
						tree, value, sel, expanded, leaf, row, hasFocus);

		if (value instanceof DefaultMutableTreeNode)
		{
			DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
			
			Object userObject = treeNode.getUserObject();
			
			if (userObject == null)
			{
				setText("unexpected null user object");
			}
			else if (userObject instanceof DiagramGroup)
			{
				setIcon(IconProvider.getIconPackage());
				setText(((DiagramGroup) userObject).getName());
			}
			else if (userObject instanceof DesktopPane.JAXBModel)
			{
				setIcon(IconProvider.getIconDocumentSmall());
				setText(((DesktopPane.JAXBModel) userObject).name);
			}
			else
			{
				setText("unexpected user object type " + userObject.getClass().getName());
			}
		}
		else
		{
			setText("unexpected value type " + value.getClass().getName());
		}

		if (sel)
		{
			setOpaque(false);
//			setBackground(getBackgroundSelectionColor());
//			setForeground(getTextSelectionColor());
			result.setBackground(Color.BLUE);
			result.setForeground(Color.WHITE);
		}
		else
		{
			setOpaque(true);
//			setBackground(getBackgroundNonSelectionColor());
//			setForeground(getTextNonSelectionColor());
			result.setBackground(Color.WHITE);
			result.setForeground(Color.BLACK);
		}

		return result;
  }
}